(function($, doc) {
	$.init({
		swipeBack: false
	});
	var state = app.getState() || {};
	var toMain = function() {
		location.href = 'index.html';
	};
	var langsets = localStorage.getItem('defaultLan');
	
	if(state.user_id) {
		//console.log(state.user_id);
		toMain();
	}
	doc.getElementById('phone').value=localStorage.getItem('old_phone') || '';
	
	var langsets = localStorage.getItem('defaultLan');
	if(langsets=='en'){
	    	doc.getElementById('now_langage').innerHTML='English';
	    }
	if(langsets=='zh'){
	    	doc.getElementById('now_langage').innerHTML='中文';
	    }
	    
	    
        	//点击多语言
		doc.getElementById('change_lang').addEventListener('tap', function() {
			doc.getElementById('lang-zz').style.display='block';
		})
		doc.getElementById('quxiao').addEventListener('tap', function() {
			doc.getElementById('lang-zz').style.display='none';
		})
		doc.getElementById('contain').addEventListener('tap', function() {
			if(!doc.querySelector('.lang-main .langbox .mui-icon-checkmarkempty')){
				doc.getElementById('lang-zz').style.display='none';
			}
			else{
				var selected=doc.querySelector('.lang-main .langbox .mui-icon-checkmarkempty');
				var sid=selected.getAttribute('id');
				var langset = localStorage.getItem('defaultLan');
				if(sid=='zh_lang' && langset=='zh'){
					doc.getElementById('lang-zz').style.display='none';
					return;
				}
			    if(sid=='en_lang' && langset=='en'){
					doc.getElementById('lang-zz').style.display='none';
					return ;
				}
			    if(sid=='zh_lang' && langset!='zh'){
					localStorage.setItem('defaultLan', 'zh');
					mui.alert('设置成功！');
					plus.runtime.restart();
				}
				if(sid=='en_lang' && langset!='en'){
					localStorage.setItem('defaultLan', 'en');
					mui.alert('设置成功！');
					plus.runtime.restart();
				}
			}
		})
	mui("#lang-zz").on('tap','.lang-main .langbox',function(){
			var type=this.getAttribute('data-lang');
			var span_arr = document.querySelectorAll('#lang-zz .lang-main .langbox span');
			mui.each(span_arr,function(index,item){
			   item.className='mui-icon';
			});
			doc.getElementById(type+'_lang').className='mui-icon mui-icon-checkmarkempty';
		})
	
	
	$.plusReady(function() {
		//仅支持竖屏显示
	    plus.screen.lockOrientation("portrait-primary");
		var loginButton = doc.getElementById('login');
		var phoneBox = doc.getElementById('phone');
		var passwordBox = doc.getElementById('password');
		
		loginButton.addEventListener('tap', function(){
			var loginInfo = {
				account: phoneBox.value,
				password: passwordBox.value,
				_token:app.token(18)
			};
			
			if(loginInfo.account=='') {
				plus.nativeUI.toast('请输入会员手机或ID');
				return;
			}
			
			if(loginInfo.password.length < 6) {
				plus.nativeUI.toast('密码最短为 6 个字符');
				return;
			}

			mui.ajax({
				url: loginurl,
				data: loginInfo,
				async: true,
				dataType: 'json',
				crossDomain: true, //强制使用5+跨域
				type: 'post',
				timeout: 10000,
				success: function(res) {
					console.log(JSON.stringify(res));
					// 请求成功
					if(res.status < 1) {
						mui.toast(res.msg);
					} else {
						mui.toast('登陆成功');
						app.createState(res.userinfo);
						localStorage.setItem('old_phone',loginInfo.account);
						location.href = 'index.html';
					}
				},
				error: function(xhr, type, errorThrown) {
					//console.log(JSON.stringify(xhr));
					//console.log(JSON.stringify(type));
					//console.log(JSON.stringify(errorThrown));
					mui.toast('操作失败！');
				}
			});
			
		}, false);
		
		
        /***
		 * 忘记密码
		 */
		doc.getElementById('forgetPassword').addEventListener('tap', function(){
			 //打开找回密码页
			  mui.openWindow({
			    id:'forgetpwd.html',
			    url:'pages/setting/forgetpwd.html',
			    show: {
					aniShow: 'pop-in',
					autoShow:false
				},
				waiting: {
					autoShow:true,//自动显示等待框，默认为true
                    title:'正在加载...',//等待对话框上显示的提示内容
				}
			  });
		}, false);
		
		
		/***
		 * 注册
		 */
		doc.getElementById('reg').addEventListener('tap', function(){
			 //打开注册页
			  mui.openWindow({
			    id:'regist.html',
			    url:'regist.html',
			    show: {
					aniShow: 'pop-in'
				},
				waiting: {
					autoShow: false
				}
			  });
		}, false);
		
		var backButtonPress = 0;
		mui.back = function() {
			backButtonPress++;
			if(backButtonPress > 1) {
				plus.runtime.quit();
			} else {
				plus.nativeUI.toast('再按一次退出应用');
			}
			setTimeout(function() {
				backButtonPress = 0;
			}, 1000);
		}
	
	});
}(mui, document));